﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDefineGlobal;
using Plugin_Map;

namespace BUS_LAYER
{
    public class BUS_Utility
    {
        public static int XMazeToXScreen(int c)   // convert a maze column to x pixel position
        { return (c * CDefine.TILE_SIZE) + CDefine.XTOP_LEFT; }

        public static int YMazeToYScreen(int r)   // convert maze row to Y pixel position
        { return (r * CDefine.TILE_SIZE) + CDefine.YTOP_LEFT; }

        public static int XScreenToXMaze(int x)   // convert X pixel position to maze column
        { return (x - CDefine.XTOP_LEFT) / CDefine.TILE_SIZE; }

        public static int YScreenToYMaze(int y)   // convert Y pixel position to maze row
        { return (y - CDefine.YTOP_LEFT) / CDefine.TILE_SIZE; }

        public static bool CanMove(int x, int y, CMap map)
        {
            int c1, r1, c2, r2, c3, r3, c4, r4;  // maze object row and column corners

            c1 = XScreenToXMaze(x); r1 = YScreenToYMaze(y);
            c2 = XScreenToXMaze(x + CDefine.TILE_SIZE - 1); r2 = YScreenToYMaze(y);
            c3 = XScreenToXMaze(x); r3 = YScreenToYMaze(y + CDefine.TILE_SIZE - 1);
            c4 = XScreenToXMaze(x + CDefine.TILE_SIZE - 1); r4 = YScreenToYMaze(y + CDefine.TILE_SIZE - 1);

            try
            {
                if ((map.IMap.ArrTile[r1, c1] == CDefine.TILE_BRICK) ||
                    (map.IMap.ArrTile[r2, c2] == CDefine.TILE_BRICK) ||
                    (map.IMap.ArrTile[r3, c3] == CDefine.TILE_BRICK) ||
                    (map.IMap.ArrTile[r4, c4] == CDefine.TILE_BRICK) ||
                    (map.IMap.ArrTile[r1, c1] == CDefine.TILE_STONE) ||
                    (map.IMap.ArrTile[r2, c2] == CDefine.TILE_STONE) ||
                    (map.IMap.ArrTile[r3, c3] == CDefine.TILE_STONE) ||
                    (map.IMap.ArrTile[r4, c4] == CDefine.TILE_STONE))
                    return false; // collision with wall, do not allow
                else
                    return true;  // no collision, proceed
            }
            catch (Exception)
            {
                return false;   // collision with wall, do not allow
            }

        } // end CanMove


    }
}
